<template>
  <div class="contain-login">
    <div class="login-form">
      <div class="login-header">电子报告智能化管理平台</div>
      <div class="login-body">
        <el-form ref="form" label-width="0px" :model="form" :rules="rules">
          <el-form-item prop="phone">
            <el-input placeholder="请输入手机号或学号" v-model="form.phone">
              <i slot="prefix" class="el-input__icon el-icon-mobile-phone"></i>
            </el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input type="password" show-password placeholder="请输入密码" style="margin-top: 20px"
                      v-model="form.password">
              <i slot="prefix" class="el-input__icon el-icon-lock"></i>
            </el-input>
          </el-form-item>
        </el-form>
      </div>
      <div class="login-btn">
        <el-button type="primary" style="width: 100%" @click="getInHome('form')">登 录</el-button>
      </div>
      <div class="login-btn">
        <el-button type="text" style="width: 100%" @click="getInStudentCode">注册账户</el-button>
        <el-button type="text" style="width: 100%" @click="getInLogon">手机号注册</el-button>
      </div>
    </div>
  </div>
</template>

<script>
import {userLogin} from "../api/aboutLogin";

export default {
  name: "Login",
  data() {
    return {
      form: {
        phone: '',
        password: ''
      },
      rules: {
        phone: [
          {required: true, message: '请输入账户名称', trigger: 'blur'},
        ],
        password: [
          {required: true, message: '请输入账户密码', trigger: 'blur'},
        ],
      }
    }
  },
  methods: {
    //手机号注册页
    getInLogon() {
      this.$router.replace('/logon')
    },
    //学号注册
    getInStudentCode() {
      this.$router.replace('/studentCode')
    },
    //	登录成功
    getInHome(form) {
      this.$refs[form].validate((valid) => {
        if (valid) {
          userLogin(this.form).then(res => {
            if (res.data.code === 200) {
              this.$message.success(res.data.message)
              window.sessionStorage.setItem('user', JSON.stringify(res.data.data.user))
              window.sessionStorage.setItem('token', res.data.data.tokenInfo.tokenValue)
              this.$router.replace('/home')
            }
          })
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    }
  }
}
</script>

<style scoped lang="less">
.contain-login {
  position: relative;
  width: 100vw;
  height: 100vh;
  background: url("../assets/loginImg/banner.png") no-repeat center;
  background-size: 100%;

  .login-form {
    position: absolute;
    right: 10%;
    top: 50%;
    transform: translateY(-50%);
    width: 20vw;
    background-color: #ffffff;
    border-radius: 10px;
    box-shadow: 0 0 10px 1px #333;
    padding: 2%;
    display: flex;
    align-items: center;
    flex-direction: column;
    justify-content: space-between;

    .login-header {
      width: 100%;
      text-align: center;
      font-size: 20px;
      letter-spacing: 1px;
      color: #4d93eb;
    }

    .login-body {
      width: 100%;
      margin: 10% 0;

      /deep/ .el-form-item {
        margin-bottom: 0;
      }
    }

    .login-btn {
      width: 100%;
      display: flex;
      justify-content: space-between;
      margin: 10px auto 0;
    }
  }
}
</style>
